<template>
  <div>
    <el-row class="line-bottom" v-show="showSearch">
      <el-col class="form_col">
        <el-form :model="queryParams" ref="queryForm" key="meterDosageQueryForm" :size="formItemSize"
                 v-show="showSearch"
                 :label-width="labelWidth">
          <div style="display: flex; justify-content: start; flex-wrap: wrap;">
            <el-form-item label="统计类型" prop="userId" style="width: 220px;">
              <el-radio-group :size="formItemSize"
                              style="width: 100%;"
                              v-model="queryParams.frozenType" @change="handleFrozenTypeChange">
                <el-radio-button label="day">日用量</el-radio-button>
                <el-radio-button label="month">月用量</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="冻结时间" prop="timevalue" v-if="queryParams.frozenType==='day'" style="width: 300px;">
              <el-date-picker :size="formItemSize"
                              style="width: 100%;"
                              v-model="timevalue"
                              type="daterange"
                              value-format="yyyy-MM-dd"
                              :clearable="false"
                              range-separator="至"
                              start-placeholder="开始日期"
                              end-placeholder="结束日期"
                              @change="handleQuery"/>
            </el-form-item>
            <el-form-item label="冻结时间" prop="timevalue" v-if="queryParams.frozenType==='month'">
              <el-date-picker :size="formItemSize"
                              style="width: 100%;"
                              v-model="timevalue"
                              :clearable="false"
                              type="monthrange"
                              value-format="yyyy-MM"
                              range-separator="至"
                              start-placeholder="开始日期"
                              end-placeholder="结束日期"
                              @change="handleQuery"/>
            </el-form-item>
          </div>
        </el-form>
      </el-col>

      <el-col class="form_query">
        <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
        <el-button @click="resetQuery" size="mini">重置</el-button>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <RightDropdown :dayColumns="dayColumns" v-if="queryParams.frozenType==='day'"
                     :showSearch.sync="showSearch" @queryTable="getList"
                     @export="handleExport"></RightDropdown>
      <RightDropdown :dayColumns="monthColumns" v-if="queryParams.frozenType==='month'"
                     :showSearch.sync="showSearch" @queryTable="getList"
                     @export="handleExport"></RightDropdown>
    </el-row>

    <el-row>
      <el-table v-loading="loading" :data="dayDataList" size="mini" border ref="dayTable"
                v-if="queryParams.frozenType==='day'"
                :height="getAvailableHeight">
        <el-table-column v-for="item in dayColumns" :key="item.key" :prop="item.dataindex" align="center"
                         :show-overflow-tooltip="true" :label="item.label" v-if="item.visible">
        </el-table-column>
      </el-table>
      <el-table v-loading="loading" :data="monthDataList" size="mini" border ref="monthTable"
                v-if="queryParams.frozenType==='month'"
                :height="getAvailableHeight">
        <el-table-column v-for="item in monthColumns" :key="item.key" :prop="item.dataindex" align="center"
                         :show-overflow-tooltip="true" :label="item.label" v-if="item.visible"/>
      </el-table>
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

  </div>
</template>

<script>
import moment from "moment/moment";
import {getDosageList} from "@/api/datastatistics/meterread/daymonthdosage.js"

export default {
  name: "Dosage",
  data() {
    return {
      screenHeight: null,
      loading: false,
      // 显示搜索条件
      showSearch: true,
      collapseAble: false,
      formItemSize: 'mini',
      maxShow: 4,
      labelWidth: '80px',
      // 总条数
      total: 0,
      // 表格数据
      dayDataList: [],
      // 列信息
      dayColumns: [
        // { key: 0, label: `平台主键`, visible: false, dataindex:'ReadGrowthRate' },
        {key: 0, label: `区域名称`, visible: true, dataindex: 'sheetsName'},
        {key: 1, label: `用户编号`, visible: true, dataindex: 'userId'},
        {key: 2, label: `用户名称`, visible: true, dataindex: 'userName'},
        {key: 3, label: `安装地址`, visible: true, dataindex: 'installAddress'},
        {key: 4, label: `设备编号`, visible: true, dataindex: 'terminalId'},
        {key: 5, label: `用户通讯地址`, visible: true, dataindex: 'userAddress'},
        {key: 6, label: `冻结日期`, visible: true, dataindex: 'freezeTime'},
        {key: 7, label: `冻结读数(m³)`, visible: true, dataindex: 'endReading'},
        {key: 8, label: `日用量(m³)`, visible: true, dataindex: 'cumulativeFlow'},
        // { key: 9, label: `实时时间`, visible: true, dataindex:'frozenType' },
      ],
      monthDataList: [],
      monthColumns: [
        // { key: 0, label: `平台主键`, visible: false, dataindex:'ReadGrowthRate' },
        {key: 0, label: `区域名称`, visible: true, dataindex: 'sheetsName'},
        {key: 1, label: `用户编号`, visible: true, dataindex: 'userId'},
        {key: 2, label: `用户名称`, visible: true, dataindex: 'userName'},
        {key: 3, label: `安装地址`, visible: true, dataindex: 'installAddress'},
        // { key: 4, label: `表类型`, visible: true, dataindex:'installAddress' },
        {key: 4, label: `设备编号`, visible: true, dataindex: 'terminalId'},
        {key: 5, label: `用户通讯地址`, visible: true, dataindex: 'userAddress'},
        {key: 6, label: `冻结日期`, visible: true, dataindex: 'freezeTime'},
        {key: 7, label: `冻结读数(m³)`, visible: true, dataindex: 'endReading'},
        {key: 8, label: `月用量(m³)`, visible: true, dataindex: 'cumulativeFlow'},
        // { key: 9, label: `实时时间`, visible: true, dataindex:'frozenType' },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        frozenType: 'day',
        code: null,
        beginTime: null,
        endTime: null,
        userId: null,
        userName: null,
        // terminalId: null,
        locationNb: null,
        communicateAddress: null,
      },
      // 冻结日期
      timevalue: [moment().subtract(30, 'day').format('YYYY-MM-DD'), moment().subtract(1, 'day').format('YYYY-MM-DD')],
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? (this.fold ? 295 : 255) : 210)
    }
  },
  methods: {
    updateTerminalId(terminalId, locationNb) {
      // this.queryParams.terminalId = terminalId
      this.queryParams.locationNb = locationNb
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    // 切换用量类型
    handleFrozenTypeChange() {
      if(this.queryParams.frozenType == 'month') {
        this.timevalue = [moment().subtract(1, 'month').format('YYYY-MM'), moment().format('YYYY-MM')]
        this.handleQuery()
      }else{
        this.timevalue = [moment().subtract(30, 'day').format('YYYY-MM-DD'), moment().subtract(1, 'day').format('YYYY-MM-DD')]
        this.handleQuery()
      }
    },
    // 重置表单
    resetQuery() {
      this.timevalue = [moment().subtract(30, 'day').format('YYYY-MM-DD'), moment().subtract(1, 'day').format('YYYY-MM-DD')]
      // let terminalId = this.queryParams.terminalId
      let locationNb = this.queryParams.locationNb

      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        frozenType: 'day',
        beginTime: this.timevalue[0],
        endTime: this.timevalue[1],
        userId: null,
        // terminalId: terminalId,
        locationNb: locationNb,
        userName: null,
        communicateAddress: null,
      }
      this.dayDataList = []
      this.monthDataList = []
      this.handleQuery()
    },
    // 获取表格数据
    getList() {
      this.loading = true;
      this.queryParams.beginTime = this.timevalue[0]
      this.queryParams.endTime = this.timevalue[1]
      getDosageList(this.queryParams).then(response => {
        if (response.code === 200) {
          if (this.queryParams.frozenType === 'day') {
            this.dayDataList = response.rows
          } else {
            this.monthDataList = response.rows
          }
          this.loading = false;
          this.total = response.total
        } else {
          this.$modal.msgError("数据查看失败!")
          this.loading = false;
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datastatistics/terminalXbStatisticAnalysis/exportDayOrMonth', {
        ...this.queryParams,
      }, `户表用量统计数据.xlsx`)
    }
  }

}
</script>

<style scoped lang="scss">
.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 20%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
